Page({
  data: {
    list: [],
    deviceId: ""
  },
  async lanya() {
    try {
      // 1.初始化蓝牙模块
      const openRes = await wx.openBluetoothAdapter()
      // 2.开始搜索蓝牙设备 一定要重复的多搜索几下
      wx.startBluetoothDevicesDiscovery({
        success: (res) => {
          setTimeout(() => {
            wx.stopBluetoothDevicesDiscovery({
              success: (res) => {
                console.log('已停止搜索')
              },
            })
          }, 9000)
          wx.onBluetoothDeviceFound(({
            devices
          }) => {
            // 注意！！！安卓手机和苹果手机 搜到的蓝牙设备
            // devicesId 不一样  安卓手机显示的mac 苹果手机收到的是uuid
            devices.forEach(item => {
              if (item.advertisData) {
                const bf = item.advertisData.slice(2, 8)

                const mac = Array.prototype.map.call(new Uint8Array(bf), x => ('00' + x.toString(16)).slice(-2)).join(':')
                console.log(mac)
                item.MAC = mac.toUpperCase()
              }
            })
            this.setData({
              list: this.data.list.concat(devices)
            })
          })
        }
      })
    } catch (err) {
      console.log(err)
    }
  },
  lianjie(e) {
    const deviceId = e.currentTarget.dataset.deviceid
    console.log(deviceId)
    wx.showModal({
      title: "提示",
      content: "确认连接此设备？",
      success: res => {
        if (res.confirm) {
          // 3.连接设备
          wx.createBLEConnection({
            deviceId: deviceId,
            success: res => {
              // 连接成功后保存了id
              this.setData({
                deviceId: deviceId
              })
              console.log('连接成功')
            },
            fail: err => {
              console.log(err)
            }
          })
        }
      }
    })
  },
  fuwu() {
    //4.获取蓝牙所有服务
    wx.getBLEDeviceServices({
      deviceId: this.data.deviceId,
      success: res => {
        console.log(res.services[0].uuid)
        // 5获取蓝牙低功耗设备某个服务中所有特征 
        wx.getBLEDeviceCharacteristics({
          deviceId: this.data.deviceId,
          serviceId: res.services[0].uuid,
          success: res => {
            console.log(res)
            //6.往蓝牙设备中第一次写数据
            // 初始化  
            // 向蓝牙设备发送一个0x00的16进制数据
            // let buffer = new ArrayBuffer(10)
            // let dataView = new DataView(buffer)
            // dataView.setUint8(0, 0)
            // wx.writeBLECharacteristicValue({
            //   deviceId: this.data.deviceId,
            //   serviceId: "00000001-0000-1000-8000-00805F9B34FB",
            //   characteristicId: "000284CF-F7E3-55B4-6C4C-9FD140100A16",
            //   // 这里的value是ArrayBuffer类型
            //   value: buffer,
            //   success(res) {
            //     console.log('写入成功', res.errMsg)
            //   }
            // })
            //7.开启蓝牙通知
            wx.notifyBLECharacteristicValueChange({
              deviceId: this.data.deviceId,
              serviceId: "00000001-0000-1000-8000-00805F9B34FB",
              characteristicId: "000384CF-F7E3-55B4-6C4C-9FD140100A16",
              state: true,
              success: res => {
                console.log('开启通知成功')
                //8.收到通知 因为有返回就会执行， 所以这里会执行多次
                // 做所有的返回判断
                wx.onBLECharacteristicValueChange(result => {
                  console.log('监听到值变化')
                  // ArrayBuffer转16进制字符串示例
                  function ab2hex(buffer) {
                    let hexArr = Array.prototype.map.call(
                      new Uint8Array(buffer),
                      function (bit) {
                        return ('00' + bit.toString(16)).slice(-2)
                      }
                    )
                    return hexArr.join('');
                  }
                  console.log(ab2hex(result.value))
                })
              }
            })
          }
        })
      }
    })
  },
  open() {
    let buffer2 = new ArrayBuffer(16)
    let dataView2 = new DataView(buffer2)
    dataView2.setUint8(0, 0)
    wx.writeBLECharacteristicValue({
      deviceId: this.data.deviceId,
      serviceId: "00000001-0000-1000-8000-00805F9B34FB",
      characteristicId: "000284CF-F7E3-55B4-6C4C-9FD140100A16",
      // 这里的value是ArrayBuffer类型
      value: buffer2,
      success(res) {
        console.log('写入成功', res.errMsg)
      }
    })
  },
  duqu(){
    let buffer2 = new ArrayBuffer(16)
    let dataView2 = new DataView(buffer2)
    dataView2.setUint8(0, 0x02)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    dataView2.setUint8(0, 0x00)
    wx.writeBLECharacteristicValue({
      deviceId: this.data.deviceId,
      serviceId: "00000001-0000-1000-8000-00805F9B34FB",
      characteristicId: "000284CF-F7E3-55B4-6C4C-9FD140100A16",
      // 这里的value是ArrayBuffer类型
      value: buffer2,
      success(res) {
        console.log('写入成功', res.errMsg)
      },fail:err=>{
        console.log(err)
      }
    })
  },
  close() {
    // 断开连接
    wx.closeBLEConnection({
      deviceId: this.data.deviceId,
      success: res => {
        console.log('关闭成功')
        console.log(res)
      }
    })
  },
  chakan() {
    // 查看本机连接状态 和查看目前发现的所有设备
    wx.getBluetoothDevices({
      success: res => {
        console.log(res)
      }
    })
  },
  onLoad() {

  }
})
